package com.maddyhome.idea.copyright.ui;

/*
 * Copyright - Copyright notice updater for IDEA
 * Copyright (C) 2004-2005 Rick Maddy. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.SupportCode;
import com.maddyhome.idea.copyright.options.LanguageOptions;

import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.EventListenerList;

public class FileTypeOverridePanel
{
    public FileTypeOverridePanel()
    {
        ButtonGroup group = new ButtonGroup();
        group.add(rbNoCopyright);
        group.add(rbUseTemplate);
        group.add(rbUseText);
        group.add(rbCustom);
        rbCustom.setSelected(true);

        ItemListener listener = new ItemListener()
        {
            public void itemStateChanged(ItemEvent itemEvent)
            {
                if (itemEvent.getStateChange() == ItemEvent.SELECTED)
                {
                    fireChangeEvent();
                }
            }
        };

        rbNoCopyright.addItemListener(listener);
        rbUseTemplate.addItemListener(listener);
        rbUseText.addItemListener(listener);
        rbCustom.addItemListener(listener);
    }

    public JComponent getMainComponent()
    {
        return mainComponent;
    }

    public void setOptions(int choice)
    {
        switch (choice)
        {
            case LanguageOptions.USE_NONE:
                rbNoCopyright.setSelected(true);
                break;
            case LanguageOptions.USE_TEMPLATE:
                rbUseTemplate.setSelected(true);
                break;
            case LanguageOptions.USE_TEXT:
                rbUseText.setSelected(true);
                break;
            case LanguageOptions.USE_CUSTOM:
                rbCustom.setSelected(true);
                break;
        }
    }

    public int getOptions()
    {
        if (rbNoCopyright.isSelected())
        {
            return LanguageOptions.USE_NONE;
        }
        if (rbUseTemplate.isSelected())
        {
            return LanguageOptions.USE_TEMPLATE;
        }
        if (rbUseText.isSelected())
        {
            return LanguageOptions.USE_TEXT;
        }
        return LanguageOptions.USE_CUSTOM;
    }

    public boolean isModified(int options)
    {
        return getOptions() != options;
    }

    public void addOptionChangeListener(FileTypeOverridePanelListener listener)
    {
        listenerList.add(FileTypeOverridePanelListener.class, listener);
    }

    public void removeOptionChangeListener(FileTypeOverridePanelListener listener)
    {
        listenerList.remove(FileTypeOverridePanelListener.class, listener);
    }

    private void fireChangeEvent()
    {
        Object[] listeners = listenerList.getListenerList();
        for (int i = listeners.length - 2; i >= 0; i -= 2)
        {
            if (listeners[i] == FileTypeOverridePanelListener.class)
            {
                ((FileTypeOverridePanelListener)listeners[i + 1]).optionChanged();
            }
        }
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT edit this method OR call it in your
     * code!
     */
    private void $$$setupUI$$$()
    {
        mainComponent = new JPanel();
        mainComponent.setLayout(new GridLayoutManager(4, 1, new Insets(0, 0, 0, 0), 0, 0));
        mainComponent
            .setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Template Override"));
        rbNoCopyright = new JRadioButton();
        rbNoCopyright.setText("No Copyright");
        rbNoCopyright.setMnemonic(78);
        SupportCode.setDisplayedMnemonicIndex(rbNoCopyright, 0);
        mainComponent.add(rbNoCopyright, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST,
            GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null,
            null));
        rbUseTemplate = new JRadioButton();
        rbUseTemplate.setText("Use Template");
        rbUseTemplate.setMnemonic(84);
        SupportCode.setDisplayedMnemonicIndex(rbUseTemplate, 4);
        mainComponent.add(rbUseTemplate, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST,
            GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null,
            null));
        rbUseText = new JRadioButton();
        rbUseText.setText("Use Template Text");
        rbUseText.setMnemonic(77);
        SupportCode.setDisplayedMnemonicIndex(rbUseText, 6);
        mainComponent.add(rbUseText, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST,
            GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null,
            null));
        rbCustom = new JRadioButton();
        rbCustom.setText("Fully Custom");
        rbCustom.setMnemonic(67);
        SupportCode.setDisplayedMnemonicIndex(rbCustom, 6);
        mainComponent.add(rbCustom, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_WEST,
            GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
            GridConstraints.SIZEPOLICY_FIXED, null, null, null));
    }

    private EventListenerList listenerList = new EventListenerList();

    private JRadioButton rbUseText;
    private JRadioButton rbUseTemplate;
    private JRadioButton rbNoCopyright;
    private JRadioButton rbCustom;
    private JPanel mainComponent;

}